package com.cyt.web.controller.base;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.SysUserService;
import com.cyt.wx.model.po.WxApply;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: YuShengQiang
 * @Date: 2018-11-16 17:17
 * @Description: 控制层基础方式
 */
@Controller
public class BaseController {
    private static Logger log = LoggerFactory.getLogger(BaseController.class);
    @Resource
    private SysUserService sysUserService;

    /**
     * 前台的空值保存到数据库为null
     *
     * @param webDataBinder 数据绑定对象
     * @author rong yuan
     * @date 2019/8/13 15:52
     */
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:18
     * @Description: 用户token统一验证
     */
    protected ReturnResult checkToken(String token) {
        if (StringUtil.isNotEmpty(token)) {
       /*     SysUser baseUser=sysUserService.selectEntityByToken(token);
            if(baseUser!=null){
                return new ReturnResult(baseUser);
            }*/
        }
        return new ReturnResult(200, "token不存在!");
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:18
     * @Description: 从Session中获取值
     */
    public static Object SessionAcceptObject(HttpServletRequest request, String name) {
        Object obj = request.getSession().getAttribute(name);
        log.info("session accept " + name + " value is " + obj);
        return obj;
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description: 从Session中删除
     */
    public static void SessionDeleteObject(HttpServletRequest request, String name) {
        request.getSession().removeAttribute(name);
        log.info("session delete " + name);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description: 从Session中修改/添加
     */
    public static void SessionAddOrUpdateObject(HttpServletRequest request, String name, Object value) {
        request.getSession().setAttribute(name, value);
        log.info("session AddOrUpdate " + name + " value is " + value);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description: 从Cookies中获取值
     */
    public static String CookiesAcceptObject(HttpServletRequest request, String name) {
        String value = null;
        Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name.trim())) {
                    value = cookie.getValue();
                    break;
                }
            }
        }
        log.info("Cookies Accept " + name + " value is " + value);
        return value;
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description: 从Cookies中删除
     */
    public static void CookiesDeleteObject(HttpServletRequest request, HttpServletResponse response, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    log.info("Cookies Delete " + name);
                    break;
                }
            }
        } else {
            log.info("Cookies Delete " + name + " is no exist");
        }
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description: 从Cookies中添加
     */
    public static void CookiesAddObject(HttpServletRequest request, HttpServletResponse response, String name, String value) {
        Cookie cookie = new Cookie(name.trim(), value.trim());
        cookie.setMaxAge(7 * 24 * 60 * 60);//保存一周
        cookie.setPath("/");
        response.addCookie(cookie);
        log.info("Cookies Add " + name + " value is " + value);
    }

    /**
     * @Author: YuShengQiang
     * @Date: 2018-11-16 17:19
     * @Description:从Cookies中修改
     */
    public static void CookiesUpdateObject(HttpServletRequest request, HttpServletResponse response, String name, String value) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null && !"".equals(cookies) && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    log.info("Cookies Update " + name + " value is old " + cookie.getValue());
                    cookie.setValue(value);
                    cookie.setMaxAge(7 * 24 * 60 * 60);//保存一周
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    log.info("Cookies Update " + name + " value is news " + cookie.getValue());
                    break;
                }
            }
        }
    }

    //获取当前登录用户信息
    public static SysUser acceptCurrentUser(HttpServletRequest request) {
        return (SysUser) request.getSession().getAttribute(BaseConstant.SESSION_USER);
    }

    //获取当前登录应用
    public static WxApply acceptCurrentApplyId(HttpServletRequest request) {
        return (WxApply) request.getSession().getAttribute(BaseConstant.SESSION_APPLY);
    }
}
